﻿using OnlineCourse.Model;
using OnlineCourse.Model.Entity;
using OnlineCourse.Services.IServices;
using OnlineCourse.Services.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OnlineCourse.Services
{
    public class CommentService : ICommentServices
    {
        private ApplicationDbContext _db;
        public CommentService(ApplicationDbContext db)
        {
            _db = db;
        }
        /// <summary>
        /// 增加一个反馈
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="SectionId"></param>
        /// <param name="Content"></param>
        /// <param name="TheAuthorContent"></param>
        /// <param name="Like"></param>
        /// <returns></returns>
        public bool Add(Guid UserId, Guid SectionId, string Content, string TheAuthorContent, int Like)
        {
            try
            {
                Comment c = new Comment();
                c.UserId = UserId;
                c.SectionId = SectionId;
                c.Content = Content;
                c.TheAuthorContent = TheAuthorContent;
                c.Like = Like;
                _db.Comments.Add(c);
                _db.SaveChanges();
                return true;
            }
            catch (Exception error)
            {
                throw error;
            }
        }
        /// <summary>
        /// 删除一个反馈
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool Delete(Guid ID)
        {
            try
            {
                var a = _db.Comments.FirstOrDefault(p => p.Id == ID);
                if (a != null)
                {
                    _db.Comments.Remove(a);
                    _db.SaveChanges();
                }
                return true;

            }
            catch (Exception error)
            {

                throw error;
            }
        }
        /// <summary>
        /// 按照用户id分页查找反馈
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="total"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IList<CommentModel> QueryLists(Guid UserId,out int total, int page , int pageSize )
        {
            var list = _db.Comments.Where(b => b.User.Id == UserId).Select(a => (new CommentModel() { Id = a.SectionId, Content = a.Content, TheAuthorContent = a.TheAuthorContent, Like = a.Like, TheAuthorDate = a.TheAuthorDate, SectionId = a.SectionId, UserId = a.UserId }));
            var list2 = list.Skip(pageSize *( page - 1)).Take(pageSize).ToList(); 
            total = list2.Count();
            return list2;
        }
        /// <summary>
        /// 根据章节ID分页查询反馈
        /// </summary>
        /// <param name="SectionId"></param>
        /// <param name="total"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IList<CommentModel> QueryList(Guid SectionId,out int total,int page , int pageSize)
        {
            var list = _db.Comments.Where(b => b.SectionId == SectionId).Select(a => (new CommentModel() { Id = a.SectionId, Content = a.Content, TheAuthorContent = a.TheAuthorContent, Like = a.Like, TheAuthorDate = a.TheAuthorDate, SectionId = a.SectionId, UserId = a.UserId }));
            var list2 = list.Skip(pageSize * (page - 1)).Take(pageSize).ToList();
            total = list2.Count();
            return list2;
        }
    }
}
